package ru.myrtle.jst.emitter.java;

import ru.myrtle.jst.i18n.NLS;
import ru.myrtle.jst.Problem;
import ru.myrtle.jst.Project;
import ru.myrtle.jst.ast.Include;
import ru.myrtle.jst.ast.Unit;
import ru.myrtle.jst.ast.Visitor;

public abstract class IncludeAwareVisitor<ContextType> extends Visitor<ContextType> {

    protected final Project project;

    public IncludeAwareVisitor(Project project) {
        this.project = project;
    }

    @Override
    public boolean visit(Include include, ContextType context) {
        Unit includedUnit = project.getUnit(
                include.getFile()
        );

        if (includedUnit != null) {
            includedUnit.accept(
                    this, context
            );
        }
        else {
            project.addProblems(
                    new Problem(
                            include.getUnit(),
                            Problem.Severity.ERROR,
                            String.format(
                                    NLS.BUNDLE.getString(NLS.KEY_MISSING_INCLUDE), include.getFile()
                            ),
                            include.getLine(),
                            include.getColumn()
                    )
            );
        }

        return true;
    }
}
